package com.idrl.web.action;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;

import com.ambow.core.util.email.MailSendService;
import com.idrl.Const;
import com.idrl.dao.po.*;
import com.idrl.service.*;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * <p>
 * Desc：抽象基类
 * </p>
 * 
 */
public abstract class BaseAction extends ActionSupport {

	protected static final String ADD = "add";
	
	protected static final String EDIT = "edit";
	
	protected static final String LIST = "list";
	
	protected static final String INFO = "info";	
	
	protected static final String HOME = "home";

	protected static final String MSGINFO = "msginfo";
	
	public static String urlPath;
	
	private static String contextPath;
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return request;
	}
	public  HttpServletResponse getResponse() {
		HttpServletResponse response = ServletActionContext.getResponse();
		return response;
	}

	
	/**
	 * 获得当前Session
	 * 
	 */
	public Map getSession() {
		return ActionContext.getContext().getSession();
	}
	
	public final String getContextPath()
	{
		if (contextPath == null)
		{
			contextPath = getRequest().getContextPath();
		}
		return contextPath;
	}
	
	/** 取请路径前缀 */
	public final String getUrlPath()
	{
		if ( urlPath == null)
		{
			urlPath = this.getRequest().getScheme() + "://" + this.getRequest().getServerName() + (this.getRequest().getServerPort() == 80 ? "" : ":" + this.getRequest().getServerPort()) + this.getRequest().getContextPath();

		}
		return urlPath;
	}
	
	/** 是否为WAP登录方式 */
	public boolean isWapLogin()
	{
		return this.getSession().get(Const.KEY_WAP_LOGIN) != null;
	}
	
	public User getUserFromSession()
	{		
		return (User)this.getSession().get(Const.KEY_USER);
	}
	
	public void setUserToSession(User user)
	{
		this.getSession().put(Const.KEY_USER, user);
	}	
	
    
    /**
     * 验证随机数。
     * @return 0：成功   1：失败
     */
    public int validRand()
    {
		int result;
    	String sRand = (String)this.getSession().get("rand");
		if(sRand == null || !sRand.equals(this.getRequest().getParameter("rand")))
		{			
			this.getRequest().setAttribute(Const.KEY_INFO_MSG, "验证码有误，请重新输入!");			
			result = Const.FLAG_FAILURE;
		}
		else
		{			
			result = Const.FLAG_SUCCESS;
		}
		
		this.getRequest().getSession().removeAttribute("rand");  
		
		return result;
    }
    
	protected UserService userService;
	protected RemarkService remarkService;
	protected TopicService topicService;
	protected BlackService blackService;
	protected MailSendService mailSendService;	
	

	public MailSendService getMailSendService() {
		return mailSendService;
	}
	public void setMailSendService(MailSendService mailSendService) {
		this.mailSendService = mailSendService;
	}
	public void setBlackService(BlackService blackService) {
		this.blackService = blackService;
	}
	public void setRemarkService(RemarkService remarkService) {
		this.remarkService = remarkService;
	}
	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
}
